﻿using EnvDTE;
using System;
using Microsoft.VisualStudio.TextTemplating;

namespace CodeGeneration.Artech
{
    public class TransformContext
    {
        public static TransformContext Current { get; internal set; }
        public TextTransformation Transformation { get; private set; }
        public ITextTemplatingEngineHost Host { get; private set; }
        public DTE Dte { get; private set; }
        public ProjectItem TemplageProjectItem { get; private set; }

        internal TransformContext(TextTransformation transformation, ITextTemplatingEngineHost host)
        {
            Transformation = transformation;
            Host = host;
            // ReSharper disable once SuspiciousTypeConversion.Global
            Dte = (DTE) (host as IServiceProvider)?.GetService(typeof(DTE));
            TemplageProjectItem = Dte?.Solution.FindProjectItem(host.TemplateFile);
        }

        public static void EnsureContextInitialized()
        {
            if (null == Current)
            {
                throw new TransformationException("TransformContext is not initialized.");
            }
        }
    }
}